/**
 * 视野过渡效果
 * 
 * 功能：
 * 1. 处理视野渐变
 * 2. 管理过渡动画
 * 3. 优化视觉效果
 * 4. 控制过渡时间
 */
@ccclass('FogTransition')
export class FogTransition extends Component {
    @property
    private transitionSpeed: number = 2;

    @property
    private smoothing: number = 0.1;

    private currentAlpha: number = 1;
    private targetAlpha: number = 1;

    public setTargetAlpha(alpha: number): void {
        this.targetAlpha = alpha;
    }

    update(dt: number) {
        if (Math.abs(this.currentAlpha - this.targetAlpha) > this.smoothing) {
            const step = this.transitionSpeed * dt;
            if (this.currentAlpha < this.targetAlpha) {
                this.currentAlpha = Math.min(this.currentAlpha + step, this.targetAlpha);
            } else {
                this.currentAlpha = Math.max(this.currentAlpha - step, this.targetAlpha);
            }
            
            // 更新材质
            this.updateMaterialAlpha(this.currentAlpha);
        }
    }

    private updateMaterialAlpha(alpha: number): void {
        const sprite = this.getComponent(Sprite);
        if (sprite) {
            const color = sprite.color;
            color.a = alpha * 255;
            sprite.color = color;
        }
    }
} 